<?php
namespace app\face\controller;
use think\Controller; 
use think\Db;
use think\Log;
use app\service\wotuService;
class Member extends Controller
{
	public function memberlist()
	{
		$user_id=request()->get("user_id");
		if(empty($user_id))
		{
			$this->error("用户不能为空");
		}
		$memberlist=Db::table("face_user_member")
						->where("user_id",$user_id)
						->select();
		$this->assign("user_id",$user_id);
		$this->assign("memberlist",$memberlist);
		return $this->fetch("member_list");
	}

	public function upload()
	{
		$id=request()->get("id");
		$memberinfo=Db::table("face_user_member")->where("id",$id)->find();
		$this->assign("memberinfo",$memberinfo);
		return $this->fetch();
	}

	//上传图片处理
    public function uploadAction()
    {
    	$id=request()->post("id");
    	$url=url("piclist")."?id=".$id;
        $image=str_replace("data:image/jpeg;base64,","",request()->post('image'));
        $memberinfo=Db::table("face_user_member")->where("id",$id)->find();
        $guid=$memberinfo["guid"];
        $result=(new wotuService())->uploadimg($image,$guid);
        if($result["code"]=="GS_SUS600"){
        	//查询该业主的用户信息
        	$userinfo=Db::table("face_user_room")->where("user_id",$memberinfo["user_id"])->find();
           	//查询该小区下的人脸设备并授权
           	$locklist=Db::table("face_lock")->where("community_id",$userinfo["community_id"])->select();
           	$deviceKeys="";
           	foreach ($locklist as $key => $value) {
           		$deviceKeys=$deviceKeys.$value["devicekey"].",";
           	}
           	$deviceKeys=rtrim($deviceKeys,",");
            //$deviceKeys="84E0F420854401F8,84E0F42085C601F8,84E0F42088D301F8,84E0F4213A2901FA,84E0F42157B306B2,84E0F420386B02F8";
            (new wotuService())->accredit($guid, $deviceKeys);
            
            echo "<script>alert('图片上传成功');location.href='$url';</script>";
        }else{
            echo "<script>alert('图片上传失败，请重新上传');history.go(-1);</script>";
            die;
        }
    }
	//图片列表
	public function piclist()
	{
		$id=request()->get("id");
		//查询该成员信息
		$memberinfo=Db::table("face_user_member")
					->where("id",$id)
					->find();
		if(empty($memberinfo))
		{
			$this->error("该用户不存在");
		}
		$guid=$memberinfo["guid"];
		if($memberinfo["guid"]=="0")
		{
			//查询用户信息
			$userinfo=Db::table("face_user_room")->where("user_id",$memberinfo["user_id"])->find();
			$type="11";			
			$re=(new wotuService())->reg($memberinfo["nickname"],$memberinfo["phone"],$type,$userinfo["room_address"],"");
			$result=json_decode($re,true);
			$guid=$result["data"]["guid"];
			Db::table("face_user_member")->where("id",$id)->update(["guid"=>$guid]);
		}
		
		$res=(new wotuService())->tupian($guid);
		$num=count($res["data"]);
		$this->assign("data",$res["data"]);
        $this->assign("num",$num);
		$this->assign("memberinfo",$memberinfo);
		return $this->fetch();
	}
	//添加成员
	public function memberadd()
	{
		if(request()->isPost())
		{
			$data=request()->post();
			//var_dump($data);die;
			$data["create_time"]=time();
			$res=Db::table("face_user_member")->insert($data);
			if($res)
			{
				$url=url("memberlist")."?user_id=".$data["user_id"];
            	echo "<script>alert('添加成功');window.location.href='$url';</script>";
				exit;
			}else
			{
				$url=url("memberlist")."?user_id=".$data["user_id"];
				echo "<script>alert('添加失败');window.location.href='$url';</script>";
				exit;
			}
		}else
		{
			$user_id=request()->get("user_id");
			$this->assign("user_id",$user_id);
			return $this->fetch("member_add");
		}
		
	}

	public function delimage()
	{
        $guid=request()->post("photoid");
        $personGuid=request()->post("personGuid");
        $result=(new wotuService())->delimage($personGuid,$guid);
        //图片删除成功
        if($result["code"]=="GS_SUS601"){
            
            $arr=array("errcode"=>"0","msg"=>"图片删除成功");
            echo json_encode($arr);
        }else{
            $arr=array("errcode"=>"1","msg"=>"图片删除失败");
            echo json_encode($arr);
        }
    }
}